﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//
namespace Serotonin.OptionsForms
{
    using Serotonin.Filter;
    using System;
    using System.Drawing;
    using System.Globalization;
    using System.Windows.Forms;
    using TF = Serotonin.Filter.ThresholdAndBinarization.Variants;

    /// <summary>Filter specified properties form.</summary>
    public partial class ThresholdAndBinarizationForm : Form, IOptionsForm
    {
        private readonly ThresholdAndBinarization filter;

        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="ThresholdAndBinarizationForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        public ThresholdAndBinarizationForm(Processor processor)
        {
            InitializeComponent();
            filter = AOF.Init(processor, this, gbProcess,
                    new ThresholdAndBinarization()) as ThresholdAndBinarization;

            SetGuiVisibility(false);
        }

                /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            // grayscale example
            Bitmap b = filter.Apply(Processor.OriginalImage);

            string sThresholds = filter.ThresholdFilter == TF.BlackWhite ||
                                    filter.ThresholdFilter == TF.T
                                        ? " T=" + filter.ThresholdBW    
                                        : "R=" + filter.ThresholdRed +
                                        " G=" + filter.ThresholdGreen +
                                        " B=" + filter.ThresholdBlue;

            string s = Program.L.Text[40] + Program.L.Text[98] + ": " +
                        Program.L.Text[99] + "=" +
                        filter.ThresholdFilter.ToString() + "; " +
                        Program.L.Text[104] + ": " + sThresholds + "; ";
            Processor.Change(b, s); 
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[98]; // Name of filter
            tabctrlOptions.TabPages[0].Text = Program.L.Text[57]; // Options

            gbColorChoice.Text = Program.L.Text[99]; 
            rdBtnRgb.Text = Program.L.Text[100];
            rdBtnRgbBinary.Text = Program.L.Text[101];
            rdBtnBW.Text = Program.L.Text[102];
            rdBtnGrayscale.Text = Program.L.Text[103];             
            gbThresholds.Text = Program.L.Text[104];             
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {
            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }

        private void RdBtnRgbCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnRgb.Checked)
            {
                SetGuiVisibility(true);
                filter.ThresholdFilter = TF.Rrb;
                Processor.ApplyFilterRTP();
            }
        }

        private void RdBtnRgbBinaryCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnRgbBinary.Checked)
            {
                SetGuiVisibility(true);
                filter.ThresholdFilter = TF.RrbBinary;
                Processor.ApplyFilterRTP();
            }
        }

        private void RdBtnBWCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnBW.Checked)
            {
                SetGuiVisibility(false);
                filter.ThresholdFilter = TF.BlackWhite;
                Processor.ApplyFilterRTP();
            }
        }

        private void RdBtnGrayscaleCheckedChanged(object sender, EventArgs e)
        {
            if (rdBtnGrayscale.Checked)
            {
                SetGuiVisibility(false);
                filter.ThresholdFilter = TF.T;
                Processor.ApplyFilterRTP();
            }
        }

        private void TrBarRedValueChanged(object sender, EventArgs e)
        {
            filter.ThresholdRed = (byte)trBarRed.Value;
            lbRedValue.Text = filter.ThresholdRed.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void TrBarGreenValueChanged(object sender, EventArgs e)
        {
            filter.ThresholdGreen = (byte)trBarGreen.Value;
            lbGreenValue.Text = filter.ThresholdGreen.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void TrBarBlueValueChanged(object sender, EventArgs e)
        {
            filter.ThresholdBlue = (byte)trBarBlue.Value;
            lbBlueValue.Text = filter.ThresholdBlue.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void TrBarRgbValueChanged(object sender, EventArgs e)
        {
            trBarRed.Value = trBarRgb.Value;
            trBarGreen.Value = trBarRgb.Value;
            trBarBlue.Value = trBarRgb.Value;
            lbRgbValue.Text = trBarRgb.Value.ToString(
                CultureInfo.InvariantCulture);
        }

        private void TrBarThresholdBwValueChanged(object sender, EventArgs e)
        {
            filter.ThresholdBW = (byte)trBarThresholdBw.Value;
            lbThresholdBwValue.Text = filter.ThresholdBW.ToString(
                CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void SetGuiVisibility(bool useRgb)
        {
            lbThresholdBwText.Enabled = !useRgb;
            lbThresholdBwValue.Enabled = !useRgb;
            trBarThresholdBw.Enabled = !useRgb;

            lbRedText.Enabled = useRgb;
            lbRedValue.Enabled = useRgb;
            trBarRed.Enabled = useRgb;

            lbGreenText.Enabled = useRgb;
            lbGreenValue.Enabled = useRgb;
            trBarGreen.Enabled = useRgb;

            lbBlueText.Enabled = useRgb;
            lbBlueValue.Enabled = useRgb;
            trBarBlue.Enabled = useRgb;

            lbRgbText.Enabled = useRgb;
            lbRgbValue.Enabled = useRgb;
            trBarRgb.Enabled = useRgb;            
        }
    }
}
